package com.neborosoft.FlyingDog.android.gui.adapters.listview;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.neborosoft.FlyingDog.R;
import com.neborosoft.FlyingDog.logic.database.content.identified.PlayList;
import com.neborosoft.android.gui.adapters.listview.ViewArrayAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 04.11.12
 * Time: 21:49
 * To change this template use File | Settings | File Templates.
 */
public class PlayListsAdapter extends ViewArrayAdapter<PlayList,PlayListsAdapterViewHolder> {
    private static final ImageLoader IMAGE_LOADER = ImageLoader.getInstance();

    private ImagesGetter imagesGetter;

    public static interface ImagesGetter{
        String[] getPlayListImagesUrls(PlayList playList, int arrayLength);
    }

    @Override
    protected int getRootLayoutId() {
        return R.layout.playlist_folder;
    }

    @Override
    protected PlayListsAdapterViewHolder createViewHolder(View view) {
        PlayListsAdapterViewHolder viewHolder = new PlayListsAdapterViewHolder();

        int[] ides = PlayListsAdapterViewHolder.imagesIdes;
        viewHolder.folderImages = new ImageView[PlayListsAdapterViewHolder.IMAGES_COUNT];

        for(int i = 0; i < ides.length; i++){
            viewHolder.folderImages[i] = (ImageView)view.findViewById(ides[i]);
        }

        viewHolder.name = (TextView)view.findViewById(R.id.playListFolderText);

        return viewHolder;
    }

    public ImagesGetter getImagesGetter() {
        return imagesGetter;
    }

    public void setImagesGetter(ImagesGetter imagesGetter) {
        this.imagesGetter = imagesGetter;
    }

    @Override
    public void setElements(Iterable<PlayList> playLists) {
        super.setElements(playLists);
    }

    private void setPlayListFolderName(PlayList playList, PlayListsAdapterViewHolder  viewHolder){
        String name = playList.getName();
        viewHolder.name.setText(name);
    }

    @Override
    protected void reuseView(PlayList playList, PlayListsAdapterViewHolder viewHolder, int position) {
        setPlayListFolderName(playList, viewHolder);
        setPlayListFolderImages(position,viewHolder);
    }

    private void setPlayListFolderImages(int position, PlayListsAdapterViewHolder  viewHolder){
        String[] bitmapUrls;
        if(imagesGetter == null){
            bitmapUrls = new String[PlayListsAdapterViewHolder.IMAGES_COUNT];
        } else {
            PlayList playList = getElement(position);
            bitmapUrls = imagesGetter.getPlayListImagesUrls(playList, PlayListsAdapterViewHolder.IMAGES_COUNT);
            if(bitmapUrls == null || bitmapUrls.length != PlayListsAdapterViewHolder.IMAGES_COUNT){
                throw new IllegalArgumentException();
            }
        }

        for(int i = 0; i < PlayListsAdapterViewHolder.IMAGES_COUNT; i++){
            String artUrl = bitmapUrls[i];
            IMAGE_LOADER.displayImage(artUrl,viewHolder.folderImages[i]);
        }
    }

    public PlayListsAdapter(Context context, ListView parent) {
        super(context, parent);
    }
}
